package com.nk.Res;

import com.nk.enums.ResponseCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Data
public class Result<T> {
    private static final String DEFAULT_MESSAGE = "";
    private long timestamp;
    private String traceId;
    private int code;
    private String message;
    private T data;
    private Map<String, String> headers;

    public Result() {
        this.timestamp = System.currentTimeMillis();
        this.traceId = UUID.randomUUID().toString();
        this.headers = new HashMap<>();
    }

    public Result(ResponseCode code, T data, String message, Map<String, String> headers) {
        this();
        this.code = code.code();
        this.data = data;
        this.message = (message == null ? DEFAULT_MESSAGE : message);
        if (headers != null && !headers.isEmpty()) {
            this.headers.putAll(headers);
        }
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ResponseCode.SUCCESS, data, DEFAULT_MESSAGE, null);
    }

    public static <T> Result<T> fail(ResponseCode code, String message) {
        return new Result<>(code, null, message, null);
    }

}


